package com.example.xlzx.config;



import com.example.xlzx.interceptor.AuthenticationInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {


    @Autowired
    private AuthenticationInterceptor authenticationInterceptor;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(this.authenticationInterceptor).addPathPatterns("/xlzx/**")
                                                                .excludePathPatterns("/xlzx/login")
                .excludePathPatterns("/xlzx/insetUser")
                .excludePathPatterns("/xlzx/upload")
                                                               ;
    }
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        ApplicationHome applicationHome = new ApplicationHome(this.getClass());
        String url = applicationHome.getDir().getParentFile().getParentFile().getAbsolutePath() + "\\src\\main\\resources\\static\\images\\";
        String convertedPath = url.replace("\\", "/");

        registry.addResourceHandler("/image/**") //虚拟url路径
                //.addResourceLocations("file:D:/images/");
                .addResourceLocations("file:" + convertedPath); //真实本地路径
    }
}
